package com.nisco.dms.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nisco.dms.entity.Menu;
import com.nisco.dms.exception.ServiceException;
import com.nisco.dms.repository.MenuDao;
import com.nisco.dms.util.dwz.Page;
import com.nisco.dms.util.dwz.PageUtils;
/**
 * 
 * @author joey
 *
 */
@Component
@Transactional(readOnly = true)
public class MenuService extends BaseService<Menu, String> {

	private MenuDao moduleDao;

	public MenuService() {

	}

	@Autowired
	public MenuService(MenuDao moduleDao) {
		super((JpaRepository<Menu, String>)moduleDao);
		this.moduleDao = moduleDao;
	}


	@Transactional
	public void delete(String id) throws ServiceException {
		Menu menu = this.get(id);
		if (isRoot(menu.getParent().getId())) {
			throw new ServiceException("不允许删除根模块。");
		}
		
		//先判断是否存在子模块，如果存在子模块，则不允许删除
		if(menu.getChildren().size() > 0){
			throw new ServiceException(menu.getStrName() + "模块下存在子模块，不允许删除。");
		}
		
		moduleDao.delete(menu);
	}


	public List<Menu> find(String fatherId, Page page) {
		org.springframework.data.domain.Page<Menu> p = 
				moduleDao.findByParentId(fatherId, PageUtils.createPageable(page));
		PageUtils.injectPageProperties(page, p);
		return p.getContent();
	}


	public List<Menu> find(String fatherId, String name, Page page) {
		org.springframework.data.domain.Page<Menu> p = 
				moduleDao.findByParentIdAndStrNameContaining(fatherId, name, PageUtils.createPageable(page));
		PageUtils.injectPageProperties(page, p);
		return p.getContent();
	}

	/**
	 * 判断是否是根模块.
	 */
	private boolean isRoot(String id) {
		return id.equals("0");
	}

	
	public Menu getTree() {
		List<Menu> list = moduleDao.findAllWithCache();
		
		List<Menu> rootList = makeTree(list);
				
		return rootList.get(0);
	}
	
	public  Menu  findOne(String id)
	{
		return moduleDao.findOne(id);
	}

	private List<Menu> makeTree(List<Menu> list) {
		List<Menu> parent = new ArrayList<Menu>();
		// get parentId = null;
		for (Menu e : list) {
			if (e.getParent() == null) {
				e.setChildren(new ArrayList<Menu>(0));
				parent.add(e);
			}
		}
		// 删除parentId = null;
		list.removeAll(parent);
		
		makeChildren(parent, list);
		
		return parent;
	}
	
	private void makeChildren(List<Menu> parent, List<Menu> children) {
		if (children.isEmpty()) {
			return ;
		}
		
		List<Menu> tmp = new ArrayList<Menu>();
		for (Menu c1 : parent) {
			for (Menu c2 : children) {
				c2.setChildren(new ArrayList<Menu>(0));
				if (c1.getId().equals(c2.getParent().getId())) {
					c1.getChildren().add(c2);
					tmp.add(c2);
				}
			}
		}
		
		children.removeAll(tmp);
		
		makeChildren(tmp, children);
	}

	/*
	public static void main(String[] args) {
		Module module = new Module();
		module.setId(1L);
		module.setName("module_1");

		Module module2 = new Module();
		module2.setId(2L);
		module2.setName("module_2");
		module2.setParent(module);

		Module module3 = new Module();
		module3.setId(3L);
		module3.setName("module_3");
		module3.setParent(module);

		Module module4 = new Module();
		module4.setId(4L);
		module4.setName("module_4");
		module4.setParent(module2);

		Module module5 = new Module();
		module5.setId(5L);
		module5.setName("module_5");
		module5.setParent(module4);

		List<Module> list = new ArrayList<Module>();
		list.add(module2);
		list.add(module3);
		module.setChildren(list);

		list = new ArrayList<Module>();
		list.add(module4);
		module2.setChildren(list);

		list = new ArrayList<Module>();
		list.add(module5);
		module4.setChildren(list);

		System.out.println(module);
		list = module.getChildren();
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
			List<Module> list2 = list.get(i).getChildren();
			for (int j = 0; j < list2.size(); j++) {
				System.out.println(list2.get(j));
				List<Module> list3 = list2.get(j).getChildren();
				for (int j2 = 0; j2 < list3.size(); j2++) {
					System.out.println(list3.get(j2));
				}
			}
		}
	}*/
	@Transactional
	public void  deleMenuByInfo(String programId,String fatherId)
	{
		moduleDao.deleMenuByInfo(programId, fatherId);
	}
}
